﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.ComponentModel;

namespace ExcelUtilities.Data
{
    /// <summary>
    /// Enumerable扩展函数
    /// </summary>
    public static class EnumerableExtensions
    {
        /// <summary>
        /// IEnumerable转WorkbookData
        /// </summary>
        /// <param name="enumerable">要转换的IEnumerable对象</param>
        /// <param name="sheetName">工作表名</param>
        /// <param name="rowDataHandler">行处理函数</param>
        /// <returns></returns>
        public static WorkbookData ToWorkbookData(this IEnumerable enumerable, string sheetName = "", Action<Dictionary<string, CellValue>> rowDataHandler = null)
        {
            WorkbookData workbookData = new WorkbookData();
            var tableData = new TableData();
            PropertyDescriptorCollection propertys = null;
            foreach (var item in enumerable)
            {
                var rowData = new Dictionary<string, CellValue>();
                if (propertys == null)
                    propertys = TypeDescriptor.GetProperties(item);
                foreach (PropertyDescriptor prop in propertys)
                {
                    rowData.Add(prop.Name, new CellValue(prop.GetValue(item)));
                }
                if (rowDataHandler != null)
                {
                    rowDataHandler(rowData);
                }
                tableData.RowDatas.Add(rowData);
            }
            workbookData.SheetDatas.Add(new WorksheetData() { Name = sheetName });
            workbookData.SheetDatas[0].TableDatas.Add(tableData);

            return workbookData;
        }
        /// <summary>
        /// IEnumerable<T>转WorkbookData
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="enumerable">要转换的IEnumerable对象</param>
        /// <param name="sheetName">工作表名</param>
        /// <param name="rowDataHandler">行处理函数</param>
        /// <returns></returns>
        public static WorkbookData ToWorkbookData<T>(this IEnumerable<T> enumerable, string sheetName = "", Action<Dictionary<string, CellValue>> rowDataHandler = null)
        {
            WorkbookData workbookData = new WorkbookData();
            var tableData = new TableData();
            var propertys = TypeDescriptor.GetProperties(typeof(T));
            foreach (PropertyDescriptor prop in propertys)
            {
                var rowData = new Dictionary<string, CellValue>();
                foreach (var item in enumerable)
                {
                    rowData.Add(prop.Name, new CellValue(prop.GetValue(item)));

                }
                if (rowDataHandler != null)
                {
                    rowDataHandler(rowData);
                }
                tableData.RowDatas.Add(rowData);
            }
            workbookData.SheetDatas.Add(new WorksheetData() { Name = sheetName });
            workbookData.SheetDatas[0].TableDatas.Add(tableData);

            return workbookData;
        }
    }
}